﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Quail
{
    public static class IQuailControlExtensions
    {
        /// <summary>
        /// waits for a condition to occur
        /// </summary>
        /// <param name="control"></param>
        /// <param name="description">description of the wait condition</param>
        /// <param name="predicate">function that returns true when the wait condition has occurred</param>
        public static void WaitFor(this IQuailControl control, string description, Func<bool> predicate)
        {
            control.Browser.Wait(new WebBrowser.WaitParameters("WaitFor")
            {
                Delegate = predicate,
                FailMessage = "TimedOut: " + description,
                PollingInterval = 200
            });            
        }

        /// <summary>
        /// Waits for the control to exist until the Browser's default timeout.
        /// </summary>
        /// <exception cref="WebBrowser.WebBrowserException">Thrown if the control is not found within the default timeout</exception>
        public static void WaitExists(this IQuailControl control)
        {
            control.Browser.Wait(new WebBrowser.WaitParameters("WaitExists")
                             {
                                 Delegate = () => control.Exists,
                                 FailMessage = "Control " + control.Selector + " does not exist",
                                 PollingInterval = 200
                             });
        }

        /// <summary>
        /// Waits for the control to be enabled until the Browser's default timeout.
        /// </summary>
        /// <exception cref="WebBrowser.WebBrowserException">Thrown if the control is not enabled within the default timeout</exception>
        public static void WaitEnabled(this IQuailControl control)
        {
            control.Browser.Wait(new WebBrowser.WaitParameters("WaitEnabled")
            {
                Delegate = () => control.Enabled,
                FailMessage = "Control " + control.Selector + " is not enabled",
                PollingInterval = 200
            });
        }

        /// <summary>
        /// Waits for the control to be visible until the Browser's default timeout.
        /// </summary>
        /// <exception cref="WebBrowser.WebBrowserException">Thrown if the control is not visible within the default timeout</exception>
        public static void WaitVisible(this IQuailControl control)
        {
            control.Browser.Wait(new WebBrowser.WaitParameters("WaitVisible")
            {
                Delegate = () => control.Visible,
                FailMessage = "Control " + control.Selector + " did not become visible",
                PollingInterval = 200
            });
        }

        /// <summary>
        /// Waits for the control to hide until the Browser's default timeout.
        /// </summary>
        /// <exception cref="WebBrowser.WebBrowserException">Thrown if the control is not visible within the default timeout</exception>
        public static void WaitHidden(this IQuailControl control)
        {
            control.Browser.Wait(new WebBrowser.WaitParameters("WaitHidden")
            {
                Delegate = () => !control.Visible,
                FailMessage = "Control " + control.Selector + " did not become hidden",
                PollingInterval = 200
            });
        }
        /// <summary>
        /// Waits for the control to exist using the Browser's default timeout.
        /// </summary>
        /// <returns>True if the control becomes visible within the timeout</returns>
        public static bool DidControlBecomeVisible(this IQuailControl control)
        {
            return control.Browser.Wait(new WebBrowser.WaitParameters("WaitVisible")
            {
                Delegate = () => control.Visible,
                FailMessage = "Control " + control.Selector + " does not become visible",
                PollingInterval = 200,
                FailStatus = LogStatus.Information
            });
        }
    }
}
